package com.fhc.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp



data class GlobalLoadingState(
    var isLoading: Boolean = false,
    var isProgress: Boolean = false,  //用来区分画面是否初始化
    val progressPercent: Float = 0F,
    var loadingText: String? = null,
) {
}

@Composable
fun GlobalLoadingView(
    loadingState: GlobalLoadingState,
    content: @Composable () -> Unit
) {

    Box(
        modifier = Modifier.fillMaxWidth(),
    ){
        content()
        if(loadingState.isLoading){
            Box(
                modifier = Modifier.fillMaxSize().clickable(enabled = false, onClick = {}).align(Alignment.Center).background(Color.Black.copy(alpha = 0.3F)),
            ){
                Box(
                    modifier = Modifier.wrapContentSize().align(Alignment.Center)
                        .shadow(elevation =  1.dp, shape = RoundedCornerShape(12.dp))
                        .background(Color.White).padding(30.dp)
                ) {
                    Column {
                        if(loadingState.isProgress) {
                            Box(
                                modifier = Modifier.wrapContentSize().align(Alignment.CenterHorizontally)
                            ){
                                CircularProgressIndicator(
                                    modifier = Modifier.height(64.dp).width(64.dp),
                                    color = Color.Red,
                                    strokeWidth = 8.dp,
                                    trackColor = Color.Red.copy(alpha = 0.5F),
                                    progress = {loadingState.progressPercent/100f}
                                )
                                Text(modifier = Modifier.wrapContentSize().align(Alignment.Center),
                                    text = ""+loadingState.progressPercent)
                            }

                        }else{
                            CircularProgressIndicator(
                                modifier = Modifier.height(64.dp).width(64.dp).align(Alignment.CenterHorizontally),
                            )
                        }
                        if(loadingState.loadingText != null) {
                            Text(
                                text = loadingState.loadingText!!,
                                color = Color.Black,
                                style = TextStyle.Default,
                                modifier = Modifier.widthIn(max = 200.dp)
                                    .wrapContentSize().offset(y = 10.dp),
                                textDecoration = TextDecoration.LineThrough,
                            )
                        }
                    }
                }
            }
        }

    }
}
